<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['applet:appPrize:add']">新增
        </el-button>
        <el-button type="success" size="mini" @click="handleCreatePre">生成预抽奖结果
        </el-button>
        <el-button type="success" size="mini" @click="handlerSetNum">设置中奖数量
        </el-button>
        <el-button type="success" size="mini" @click="handlerInitUserIsFinale">初始化最后关卡
        </el-button>
      </el-form-item>
    </el-form>


    <el-table border stripe v-loading="loading" :data="appPrizeList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="编号" width="60" align="center" prop="id"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="照片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="120" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="概率" width="100" align="center" prop="range"/>
      <el-table-column label="数量" width="100" align="center" prop="num"/>
      <el-table-column label="字体" width="100" align="center" prop="fontColor">
        <template slot-scope="scope">
          <div
            :style="{'fontSize':scope.row.fontSize + 'px',color: scope.row.fontColor,backgroundColor:scope.row.backgroundColor }">
            {{ scope.row.fontSize }}px
          </div>
        </template>
      </el-table-column>

      <el-table-column label="更新时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="更新账号" align="center" prop="updateBy" width="150"/>
      <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:appPrize:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:appPrize:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分抽奖奖品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称"/>
            </el-form-item>
            <el-form-item label="中奖概率" prop="range">
              <el-input-number :min="0" :max="1" :precision="2" v-model="form.range" placeholder="请输入中奖概率"/>
            </el-form-item>
            <el-form-item label="数量" prop="num">
              <el-input-number :min="0" r v-model="form.num" placeholder="请输入数量"/>
            </el-form-item>
            <el-form-item label="字体大小" prop="fontSize">
              <el-input-number :min="6" :max="18" v-model="form.fontSize" placeholder="请输入字体大小"/>
            </el-form-item>
            <el-row :gutter="10">
              <el-col :span="12">
                <el-form-item label="背景颜色" prop="backgroundColor">
                  <el-color-picker v-model="form.backgroundColor"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="字体颜色" prop="fontColor">
                  <el-color-picker v-model="form.fontColor"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="12">
            <el-form-item label="照片" prop="image">
              <image-upload :limit="1" v-model="form.image"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addAppPrize,
  changeAppPrizeStatus,
  createUserPrePrize,
  delAppPrize,
  getAppPrize,
  initUserIsFinale,
  listAppPrize,
  setNowWinNum,
  updateAppPrize
} from "@/api/background/appPrize";

export default {
  name: "AppPrize",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 积分抽奖奖品表格数据
      appPrizeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        image: [
          {required: true, message: "照片不能为空", trigger: "blur"}
        ],
        range: [
          {required: true, message: "中奖概率不能为空", trigger: "blur"}
        ],
        num: [
          {required: true, message: "数量不能为空", trigger: "blur"}
        ],
        backgroundColor: [
          {required: true, message: "背景颜色不能为空", trigger: "blur"}
        ],
        fontColor: [
          {required: true, message: "字体颜色不能为空", trigger: "blur"}
        ],
        fontSize: [
          {required: true, message: "字体大小不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "0正常 1停用不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handlerInitUserIsFinale() {
      initUserIsFinale().then(res => {
        this.$modal.msgSuccess("设置成功");
      })
    },

    handlerSetNum() {
      setNowWinNum().then(res => {
        this.$modal.msgSuccess("设置成功");
      })
    },


    handleCreatePre() {
      createUserPrePrize().then(res => {
        this.$modal.msgSuccess("生成成功");
      })
    },

    handleStatusChange(row) {
      let text = row.status === "0" ? "上架" : "下架";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"的奖品吗？').then(function () {
        return changeAppPrizeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.handleQuery()
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 查询积分抽奖奖品列表 */
    getList() {
      this.loading = true;
      listAppPrize(this.queryParams).then(response => {
        this.appPrizeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        image: null,
        range: null,
        num: null,
        backgroundColor: '#E9E8FE',
        fontColor: '#232327',
        fontSize: 14,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加积分抽奖奖品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAppPrize(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改积分抽奖奖品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppPrize(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppPrize(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除积分抽奖奖品编号为"' + ids + '"的数据项？').then(function () {
        return delAppPrize(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/appPrize/export', {
        ...this.queryParams
      }, `appPrize_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
